import { Table } from 'antd';
import Cls from 'classnames';
import { useContext, useEffect, useMemo } from 'react';
import { AssetListManageContext, openMaterielColumns } from '../mconfig';
import { getLoadMapList, getSelectDataMapList, getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { IAssetListActionEnum } from './useManageReducer';

export const UpdateMaterielView: React.FC<any> = ({ pageType, setMainAsset, openAssetModalStatus }) => {
  const { selectAssetList, assetListDispatch } = useContext(AssetListManageContext);

  /**  获取数据字典 */
  const { loadMapInfo, mapLoading } = getLoadMapList([
    `${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`, //资产类型
  ]);

  /** 资产类型 */
  const assetTypeList = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`], 'name', 'id', true);

  /** 删除组合资产中的某项 */
  const delSelectRow = (record: Record<string, any>) => {
    let idx = selectAssetList.findIndex((item: Record<string, any>) => {
      return item.id === record.id;
    });
    if (idx !== -1) {
      let arr = JSON.parse(JSON.stringify(selectAssetList));
      arr.splice(idx, 1);
      assetListDispatch({
        type: IAssetListActionEnum.SELECT_ASSET,
        payload: arr,
      });
    }
  };

  /** 表格行配置 */
  const columns: Record<string, any>[] = useMemo(() => {
    return openMaterielColumns(delSelectRow, assetTypeList, selectAssetList, setMainAsset, openAssetModalStatus, pageType);
  }, [selectAssetList]);

  return (
    <div className={Cls('list-table', 'descriptionsType-tableSty')} style={{ marginTop: 10 }}>
      <Table bordered size="small" dataSource={selectAssetList} columns={columns} rowKey={(res) => res?.id} scroll={{ x: 1200, y: '' }} pagination={false} />
    </div>
  );
};
